package com.shangxian.giveithome.service.impl;

import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.UserCode;
import com.shangxian.giveithome.controller.MechanismServer;
import com.shangxian.giveithome.entity.MechanismDetails;
import com.shangxian.giveithome.entity.PutUserFei;
import com.shangxian.giveithome.entity.RoleUser;
import com.shangxian.giveithome.entity.RolesUser;
import com.shangxian.giveithome.entity.User;
import com.shangxian.giveithome.entity.UserDataUpdate;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.RolesMapper;
import com.shangxian.giveithome.mapper.UserDataMapper;
import com.shangxian.giveithome.mapper.UserMapper;
import com.shangxian.giveithome.service.FeiUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.shangxian.giveithome.properties.UserProperties.ROLE_MECHANISM;
import static com.shangxian.giveithome.properties.UserProperties.ROLE_YB_MECHANISM;
import static com.shangxian.giveithome.utils.DateTime.getDays;


@Service
@Slf4j
public class FeiUserServiceImpl implements FeiUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RolesMapper rolesMapper;

    @Resource
    private UserDataMapper userDataMapper;

    @Autowired
    private MechanismServer mechanismServer;

    /**
     * 修改角色
     *
     * @param roleUser
     */
    @Transactional
    @Override
    public void update_role(RoleUser roleUser) {
        userMapper.updateRole(roleUser);
    }

    /**
     * 根据openid查询用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public UserDetails queryByOpenId(String openId) {
        User user = userMapper.queryByOpenIdUser(openId);
        if (user == null) {
            return null;
        }
        UserDetails userDetails = new UserDetails();
        BeanUtils.copyProperties(user, userDetails);
        if (user.getRole().equals(ROLE_YB_MECHANISM)) {
            userDetails.setMechanismStatus(true);
        }
        //查看是否是机构角色
        if (user.getRole().equals(ROLE_MECHANISM)) {
            userDetails.setMechanismStatus(true);
            //查询机构信息
            MechanismDetails mechanismDetails = mechanismServer.queryByMe(user.getId());
            if (mechanismDetails == null) {
                ExceptionUtils.error(CommonCode.DATA_ERRORS);
            }
            userDetails.setMechanism(mechanismDetails);
        }
        userDetails.setDays(getDays(userDetails.getVolunteerTime()));
        return userDetails;
    }

    /**
     * 修改用户数据
     *
     * @param userDataUpdate
     */
    @Transactional
    @Override
    public void update_user_data(UserDataUpdate userDataUpdate) {
        try {
            userDataMapper.updateUserData(userDataUpdate);
        } catch (Exception e) {
            ExceptionUtils.error(UserCode.USER_DATA_ERROR);
        }
    }

    /**
     * 查看用户角色
     *
     * @param id)
     * @return
     */
    @Override
    public RolesUser get_role(Integer id) {
        return rolesMapper.getRole(id);
    }

    /**
     * 根据openid更新用户信息
     *
     * @param putUserFei
     */
    @Override
    @Transactional
    public void updateUser(PutUserFei putUserFei) {
        int i = userMapper.updateUser(putUserFei);
        if (i == 0) {
            ExceptionUtils.error(UserCode.USER_UPDATE_ERROR);
        }
    }

    @Override
    public Integer updateUserVolunteerTime(Long usId) {
        List<Long> ids = new ArrayList<Long>();
        ids.add(usId);
        User user = Optional.ofNullable(userMapper.queryById(ids)).orElse(null).stream().findFirst().orElse(null);
        if (user == null) {
            return 0;
        }
        return getDays(user.getVolunteerTime());
    }

    @Override
    @Transactional
    public void updateUserDataReduce(UserDataUpdate userDataUpdate) {
        try {
            userDataMapper.updateUserDataReduce(userDataUpdate);
        } catch (Exception e) {
            ExceptionUtils.error(UserCode.USER_DATA_ERROR);
        }
    }
}
